<!-- 员工详情页 -->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              ref="userInfoRef"
              :rules="rules"
              :model="userInfo"
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width: 300px" />
              </el-form-item>
              <el-form-item label="新密码:" prop="password2">
                <el-input
                  v-model="userInfo.password2"
                  style="width: 300px"
                  type="password"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUserInfo"
                  >更新</el-button
                >
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <!-- 个人详情 -->
          <el-tab-pane label="个人详情">
            <!-- 跳转到打印页面 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 使用动态组件 component,is属性可以直接为注册组件的组件名称即可，is必须是变量 -->
            <!-- <UserInfoDetail /> -->
            <component :is="userInfoComponents"></component>
          </el-tab-pane>

          <!-- 岗位信息 -->
          <el-tab-pane label="岗位信息">
            <!-- 跳转到打印页面 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="JobInfoComponents"></component>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailsByIdAPI } from '@/api/user'
import { saveEmployeesAPI } from '@/api/employees'
import UserInfoDetail from './components/UserInfoDetail'
import JobInfoDetail from './components/JobInfoDetail'
export default {
  components: { UserInfoDetail, JobInfoDetail },
  data() {
    return {
      userInfoComponents: 'UserInfoDetail', // 这里声明动态组件的变量
      JobInfoComponents: 'JobInfoDetail', // 这里声明动态组件的变量
      userId: this.$route.params.id, // 获取用户的id
      // 存放基本信息
      userInfo: {
        username: '',
        password2: '' // 这里使用password2的原因是读取到的密码是密文，最后保存的时候在将password2赋值给password
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetailsById()
  },
  methods: {
    // 获取用户基本信息
    async getUserDetailsById() {
      const res = await getUserDetailsByIdAPI(this.userId)
      this.userInfo = res.data
    },
    // 更新
    updateUserInfo() {
      this.$refs.userInfoRef.validate(async (valid) => {
        if (!valid) return false
        const res = await saveEmployeesAPI({
          ...this.userInfo,
          password: this.userInfo.password2
        })
        // console.log('更新成功', res)
        if (!res.success) return this.$message.error(res.message)
        this.$message.success(res.message)
      })
    }
  }
}
</script>

<style></style>
